﻿package net.wangit.support;

import net.wangit.Row;
import net.wangit.constants.ElementShowMode;
import net.wangit.context.ActionContext;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.ElementConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 元素解析类，为“定制元素”使用。
 * 
 * @author watson Feb 25, 2010
 */
public abstract class AbstractElementWriter implements ElementWriter {

	private static Log log = LogFactory
			.getLog(net.wangit.support.AbstractElementWriter.class);

	public AbstractElementWriter() {
	}

	// 将元素对象转换为HTML字符串。
	public void write(ActionContext ac, StringBuffer html, ElementComponent ec,
			Row row) {
		int showMode = ec.getShowMode();
		switch (showMode) {
		case ElementShowMode.TEXT:
			writeText(ac, html, ec, row);
			return;

		case ElementShowMode.TEXT_VALUE:
			writeTextWithValue(ac, html, ec, row);
			return;

		case ElementShowMode.CONTROL:
			writeControl(ac, html, ec, row);
			return;

		case ElementShowMode.READONLY:
			writeReadOnlyControl(ac, html, ec, row);
			return;

		case ElementShowMode.CONTROL_DISABLED:
			writeDisabledControl(ac, html, ec, row);
			return;

		case ElementShowMode.HIDDEN:
			writeHidden(ac, html, ec, row);
			return;

		case ElementShowMode.NULL:
			return;

		default:
			log.error((new StringBuilder("Unsupported elementShowMode: "))
					.append(showMode).toString());
			throw new ElementConfigurationException((new StringBuilder(
					"unsupported elementShowMode: ")).append(showMode)
					.toString(), ec.getParent().getDomain(), ec.getParent()
					.getId(), ec.getId(), "mode");
		}
	}

	// 元素显示模式：文本。
	protected abstract void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row);

	// 元素显示模式：文本带值。
	protected abstract void writeTextWithValue(ActionContext ac,
			StringBuffer html, ElementComponent ec, Row row);

	// 元素显示模式：控件。
	protected abstract void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row);

	// 元素显示模式：只读。
	protected abstract void writeReadOnlyControl(ActionContext ac,
			StringBuffer html, ElementComponent ec, Row row);

	// 元素显示模式：失效。
	protected abstract void writeDisabledControl(ActionContext ac,
			StringBuffer html, ElementComponent ec, Row row);

	// 元素显示模式：隐藏。
	protected abstract void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row);

}
